<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <template #header>
          <tree-tools :node-data="company" :is-show="false" @add-depts="handleAddDepts" />
        </template>

        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 用了一个行列布局 -->
          <template #default="{ data }">
            <tree-tools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="handleAddDepts"
              @edit-depts="handleEditDepts"
            />
          </template>
        </el-tree>

      </el-card></div>

    <!-- 新增部门弹出层 -->
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :depts-list="deptsList"
      :node-data="nodeData"
      @add-depts="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept'
import { reqGetDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'

export default {
  name: 'Departments',

  components: {
    TreeTools,
    AddDept
  },

  data() {
    return {
      // 处理后部门数据
      departs: [],

      defaultProps: {
        label: 'name',
        children: 'children'
      },

      // 头部数据
      company: {},

      // 是否展示新增部门弹层
      showDialog: false,

      // tree-tools 传过来的数据
      nodeData: {},

      // 部门列表数据
      deptsList: [],

      // 用来控制进度弹层的显示和隐藏
      loading: false
    }
  },

  created() {
    this.getDepartments()
  },

  methods: {
    // 获取部门
    async getDepartments() {
      // 开启 loading
      this.loading = true
      const { data } = await reqGetDepartments()
      // 头部数据
      this.company = {
        name: data.companyName,
        manager: '负责人',
        id: ''
      }
      // 结构数据
      this.departs = tranListToTreeData(data.depts, '')
      // 储存数据
      this.deptsList = data.depts
      // 关闭 loading
      this.loading = false
    },

    // 处理添加操作, 显示弹层
    handleAddDepts(nodeData) {
      // 显示弹层
      this.showDialog = true
      // 存储传过来的数据, 这个应该记录下来, 将来添加需要用到
      this.nodeData = nodeData
    },

    // 编辑部门
    handleEditDepts(nodeData) {
      // 显示弹层
      this.showDialog = true
      // 存储传过来的数据, 这个应该记录下来, 将来添加需要用到
      this.nodeData = nodeData
      // 获取部门详情
      this.$nextTick(() => {
        this.$refs.addDept.getDepartDetail()
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.tree-card {
  padding: 30px;
  font-size:14px;

  ::v-deep {
    .el-card__header {
      padding-bottom: 0;
      font-weight: 700;
    }
  }
}

.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
